﻿using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;

namespace AbbrWordWiki_OData
{
    public class AbbrWordWikiWcfDataService : DataService<AbbrWordWikiDBEntities>
    {
        // This method is called only once to initialize service-wide policies.
        public static void InitializeService(DataServiceConfiguration config)
        {
            config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);                         
            //This could be "*" and could also be ReadSingle, etc, etc.            
            config.SetServiceOperationAccessRule("GetAbbrWords", ServiceOperationRights.AllRead);                         
            //Set a reasonable paging site            config.SetEntitySetPageSize("*", 25);                         
            config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
        }

        protected override void OnStartProcessingRequest(ProcessRequestArgs args)        
        {            
            base.OnStartProcessingRequest(args);            
            //Cache for a minute based on querystring            
            HttpContext context = HttpContext.Current;            
            HttpCachePolicy c = HttpContext.Current.Response.Cache;            
            c.SetCacheability(HttpCacheability.ServerAndPrivate);            
            c.SetExpires(HttpContext.Current.Timestamp.AddSeconds(60));            
            c.VaryByHeaders["Accept"] = true;            
            c.VaryByHeaders["Accept-Charset"] = true;            
            c.VaryByHeaders["Accept-Encoding"] = true;            
            c.VaryByParams["*"] = true;        
        }
         
        [WebGet]
        public IQueryable<t_AbbrWordWiki> GetAbbrWords()        
        {
            var popularPosts = from p in this.CurrentDataSource.t_AbbrWordWiki                                
                                orderby p.Word                               
                                select p;             
            return popularPosts;        
        }
    }
}
